package com.am.mosl.cityandroid.login;

import java.util.regex.Matcher;
import java.util.regex.Pattern;

import org.json.JSONException;
import org.json.JSONObject;

import com.am.mosl.cityandroid.R;
import com.am.mosl.cityandroid.activity.BaseActivity;
import com.am.mosl.cityandroid.constant.HttpConstant;

import net.tsz.afinal.FinalHttp;
import net.tsz.afinal.http.AjaxCallBack;
import net.tsz.afinal.http.AjaxParams;

import android.app.ProgressDialog;
import android.content.Context;
import android.content.Intent;
import android.net.ConnectivityManager;
import android.net.NetworkInfo;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;

/**
 * @author 邓思宇 输入手机号界面
 * 
 */
public class ForgetActivity extends BaseActivity {

	private EditText phoneNum;
	private Button next;
	private ProgressDialog mDialog;
	private String FLAG;
	public static Handler h;

	
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.z_recode_activity);

		initBar();

		h = new Handler() {
			public void handleMessage(Message msg) {
				super.handleMessage(msg);

				switch (msg.what) {

				case 0:
					finish();
					break;

				}
			}

		};

		// 获取到上一个页面得来的FLAG值来判断接下是新用户还是更换密码
		Bundle extras = getIntent().getExtras();
		FLAG = extras.getString("FLAG");

		next = (Button) findViewById(R.id.login_next);
		phoneNum = (EditText) findViewById(R.id.PhoneNum);

		// 点击下一步按钮
		next.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View v) {
				// TODO Auto-generated method stub

				final String phonenum = phoneNum.getText().toString().trim();
				if (phonenum.equals("")) {
					Toast.makeText(ForgetActivity.this, "手机号不能为空",
							Toast.LENGTH_SHORT).show();
					return;
				}

				if (!isMobile(phonenum)) {
					Toast.makeText(ForgetActivity.this, "请输入正确的手机号",
							Toast.LENGTH_SHORT).show();
					return;
				}

				// 发送验证码
				if (isNetworkAvailable(ForgetActivity.this)) {

					mDialog = ProgressDialog.show(ForgetActivity.this, "",
							"正在发送验证码...", true);

					FinalHttp finalHttp = new FinalHttp();
					AjaxParams params = new AjaxParams();
					params.put("telephone", phonenum);
					finalHttp.post(HttpConstant.GetVerifyCode, params,
							new AjaxCallBack<Object>() {
								@Override
								public boolean isProgress() {
									return super.isProgress();
								}

								@Override
								public void onSuccess(Object o) {
									super.onSuccess(o);
									try {
										JSONObject jsonObject = new JSONObject(
												o.toString());
										String status = (String) jsonObject
												.opt("status");
										String msg = (String) jsonObject
												.opt("msg");
										
										
										if (mDialog != null) {
											mDialog.dismiss();
										}
										
										
										if (status.equals("1")) {

											// 传递手机号码值给下一个界面用
											String phonenumber = phonenum;
											Intent intent = new Intent(
													ForgetActivity.this,
													VertifyActivity.class);
											intent.putExtra("Phone",
													phonenumber);

											intent.putExtra("FLAG", FLAG);
											startActivity(intent);

										} else {

											Toast.makeText(ForgetActivity.this,
													" 登录失败：" + msg,
													Toast.LENGTH_SHORT).show();

											return;

										}

									

									} catch (JSONException e) {
										e.printStackTrace();
									}
								}
							});

				} else {
					Toast.makeText(ForgetActivity.this, "网络未连接",
							Toast.LENGTH_SHORT).show();
				}

			}
		});

	}

	/**
	 * 判断网络是否可用
	 * 
	 */
	public static boolean isNetworkAvailable(Context context) {
		ConnectivityManager mgr = (ConnectivityManager) context
				.getSystemService(Context.CONNECTIVITY_SERVICE);
		NetworkInfo[] info = mgr.getAllNetworkInfo();
		if (info != null) {
			for (int i = 0; i < info.length; i++) {
				if (info[i].getState() == NetworkInfo.State.CONNECTED) {
					return true;
				}
			}
		}
		return false;
	}

	/**
	 * 手机号验证
	 * 
	 * @param str
	 * @return 验证通过返回true
	 */
	public static boolean isMobile(String str) {
		Pattern p = null;
		Matcher m = null;
		boolean b = false;
		p = Pattern.compile("^[1][3,4,5,8][0-9]{9}$"); // 验证手机号
		m = p.matcher(str);
		b = m.matches();
		return b;
	}

	private void initBar() {

		TextView title = (TextView) findViewById(R.id.tittle);
		title.setText("输入手机号");

		View right_title = (TextView) findViewById(R.id.right_tittle);
		right_title.setVisibility(View.GONE);
	}

	// 点击返回
	public void BackToBack(View view) {
		this.finish();
	}
}